home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_349 / med / source / med200src.zoo / iffr.c < prev    next >
C/C++ Source or Header  |  1990-04-08  |  11KB  |  325 lines

  1. /*----------------------------------------------------------------------*
  2.  * IFFR.C  Support routines for reading IFF-85 files.          1/23/86
  3.  * (IFF is Interchange Format File.)
  4.  *
  5.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * This version for the Commodore-Amiga computer.
  9.  *
  10.  * Uses "gio".  Either link with gio.c, or set the GIO_ACTIVE flag to 0
  11.  * in gio.h.
  12.  *----------------------------------------------------------------------*/
  13. #include "iff/gio.h"
  14. #include "iff/iff.h"
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17.  
  18. /* ----- Private subroutine FileLength() --------------------------------*/
  19. /* Returns the length of the file or else a negative IFFP error code
  20.  * (NO_FILE or DOS_ERROR). AmigaDOS-specific implementation.
  21.  * SIDE EFFECT: Thanks to AmigaDOS, we have to change the file's position
  22.  * to find its length.
  23.  * Now if Amiga DOS maintained fh_End, we'd just do this:
  24.  *    fileLength = (FileHandle *)BADDR(file)->fh_End; */
  25. LONG FileLength(file)  BPTR file;  {
  26.     LONG fileLength = NO_FILE;
  27.  
  28.     if (file > 0)  {
  29.     GSeek(file, 0, OFFSET_END);    /* Seek to end of file.*/
  30.     fileLength = GSeek(file, 0, OFFSET_CURRENT);
  31.         /* Returns position BEFORE the seek, which is #bytes in file. */
  32.     if (fileLength < 0)
  33.         fileLength = DOS_ERROR;    /* DOS being absurd.*/
  34.     }
  35.  
  36.     return(fileLength);
  37.     }
  38.  
  39. /* ---------- Read -----------------------------------------------------*/
  40.  
  41. /* ---------- OpenRIFF --------------------------------------------------*/
  42. IFFP OpenRIFF(file0, new0, clientFrame)
  43.     BPTR file0;   GroupContext *new0;  ClientFrame *clientFrame; {
  44.     register BPTR file = file0;
  45.     register GroupContext *new = new0;
  46.     IFFP iffp = IFF_OKAY;
  47.  
  48.     new->parent       = NULL;        /* "whole file" has no parent.*/
  49.     new->clientFrame  = clientFrame;
  50.     new->file         = file;
  51.     new->position     = 0;
  52.     new->ckHdr.ckID   = new->subtype    = NULL_CHUNK;
  53.     new->ckHdr.ckSize = new->bytesSoFar = 0;
  54.  
  55.     /* Set new->bound and go to the file's beginning. */
  56.     new->bound = FileLength(file);
  57.     if (new->bound < 0)
  58.     iffp = new->bound;           /* File system error! */
  59.     else if ( new->bound < sizeof(ChunkHeader) )
  60.     iffp = NOT_IFF;               /* Too small for an IFF file. */
  61.     else
  62.     GSeek(file, 0, OFFSET_BEGINNING);  /* Go to file start. */
  63.  
  64.     return(iffp);
  65.     }
  66.  
  67. /* ---------- OpenRGroup -----------------------------------------------*/
  68. IFFP OpenRGroup(parent0, new0)   GroupContext *parent0, *new0; {
  69.     register GroupContext *parent = parent0;
  70.     register GroupContext *new    = new0;
  71.     IFFP iffp = IFF_OKAY;
  72.  
  73.     new->parent       = parent;
  74.     new->clientFrame  = parent->clientFrame;
  75.     new->file         = parent->file;
  76.     new->position     = parent->position;
  77.     new->bound        = parent->position + ChunkMoreBytes(parent);
  78.     new->ckHdr.ckID   = new->subtype    = NULL_CHUNK;
  79.     new->ckHdr.ckSize = new->bytesSoFar = 0;
  80.  
  81.     if ( new->bound > parent->bound  ||  IS_ODD(new->bound) )
  82.     iffp = BAD_IFF;
  83.     return(iffp);
  84.     }
  85.  
  86. /* ---------- CloseRGroup -----------------------------------------------*/
  87. IFFP CloseRGroup(context)   GroupContext *context; {
  88.     register LONG position;
  89.  
  90.     if (context->parent == NULL) {
  91.     }  /* Context for whole file.*/
  92.     else {
  93.     position = context->position;
  94.     context->parent->bytesSoFar += position - context->parent->position;
  95.     context->parent->position = position;
  96.     }
  97.     return(IFF_OKAY);
  98.     }
  99.  
  100. /* ---------- SkipFwd --------------------------------------------------*/
  101. /* Skip over bytes in a context. Won't go backwards.*/
  102. /* Updates context->position but not context->bytesSoFar.*/
  103. /* This implementation is AmigaDOS specific.*/
  104. IFFP SkipFwd(context, bytes)   GroupContext *context;  LONG bytes; {
  105.     IFFP iffp = IFF_OKAY;
  106.  
  107.     if (bytes > 0) {
  108.     if (-1 == GSeek(context->file, bytes, OFFSET_CURRENT))
  109.         iffp = BAD_IFF;    /* Ran out of bytes before chunk complete.*/
  110.     else
  111.         context->position += bytes;
  112.     }
  113.     return(iffp);
  114.     }
  115.  
  116. /* ---------- GetChunkHdr ----------------------------------------------*/
  117. ID GetChunkHdr(context0)   GroupContext *context0;  {
  118.     register GroupContext *context = context0;
  119.     register IFFP iffp;
  120.     LONG remaining;
  121.  
  122.     /* Skip remainder of previous chunk & padding. */
  123.     iffp = SkipFwd(context,
  124.     ChunkMoreBytes(context) + IS_ODD(context->ckHdr.ckSize));
  125.     CheckIFFP();
  126.  
  127.     /* Set up to read the new header. */
  128.     context->ckHdr.ckID = BAD_IFF;    /* Until we know it's okay, mark it BAD.*/
  129.     context->subtype    = NULL_CHUNK;
  130.     context->bytesSoFar = 0;
  131.  
  132.     /* Generate a psuedo-chunk if at end-of-context. */
  133.     remaining = context->bound - context->position;
  134.     if (remaining == 0) {
  135.     context->ckHdr.ckSize = 0;
  136.     context->ckHdr.ckID   = END_MARK;
  137.     }
  138.  
  139.     /* BAD_IFF if not enough bytes in the context for a ChunkHeader.*/
  140.     else if (sizeof(ChunkHeader) > remaining) {
  141.     context->ckHdr.ckSize = remaining;
  142.     }
  143.  
  144.     /* Read the chunk header (finally). */
  145.     else {
  146.         switch (
  147.         GRead(context->file, (BYTE *)&context->ckHdr, sizeof(ChunkHeader))
  148.         ) {
  149.         case -1: return(context->ckHdr.ckID = DOS_ERROR);
  150.         case 0:  return(context->ckHdr.ckID = BAD_IFF);
  151.         }
  152.  
  153.     /* Check: Top level chunk must be LIST or FORM or CAT. */
  154.     if (context->parent == NULL)
  155.         switch(context->ckHdr.ckID) {
  156.         case FORM:  case LIST:  case CAT:  break;
  157.         default:    return(context->ckHdr.ckID = NOT_IFF);
  158.         }
  159.  
  160.     /* Update the context. */
  161.     context->position += sizeof(ChunkHeader);
  162.     remaining         -= sizeof(ChunkHeader);
  163.  
  164.     /* Non-positive ID values are illegal and used for error codes.*/
  165.     /* We could check for other illegal IDs...*/
  166.     if (context->ckHdr.ckID <= 0)
  167.          context->ckHdr.ckID = BAD_IFF;
  168.  
  169.     /* Check: ckSize negative or larger than # bytes left in context? */
  170.     else if (context->ckHdr.ckSize < 0  ||
  171.          context->ckHdr.ckSize > remaining) {
  172.         context->ckHdr.ckSize = remaining;
  173.         context->ckHdr.ckID   = BAD_IFF;
  174.         }
  175.  
  176.     /* Automatically read the LIST, FORM, PROP, or CAT subtype ID */
  177.     else switch (context->ckHdr.ckID) {
  178.         case LIST:  case FORM:  case PROP:  case CAT:  {
  179.         iffp = IFFReadBytes(context,
  180.                     (BYTE *)&context->subtype,
  181.                     sizeof(ID));
  182.         if (iffp != IFF_OKAY)
  183.             context->ckHdr.ckID = iffp;
  184.         break; }
  185.         }
  186.  
  187.     }
  188.     return(context->ckHdr.ckID);
  189.     }
  190.  
  191. /* ---------- IFFReadBytes ---------------------------------------------*/
  192. IFFP IFFReadBytes(context, buffer, nBytes)
  193.     GroupContext *context;   BYTE *buffer;   LONG nBytes; {
  194.     register IFFP iffp = IFF_OKAY;
  195.  
  196.     if (nBytes < 0)
  197.     iffp = CLIENT_ERROR;
  198.     else if (nBytes > ChunkMoreBytes(context))
  199.     iffp = SHORT_CHUNK;
  200.     else if (nBytes > 0)
  201.     switch ( GRead(context->file, buffer, nBytes) ) {
  202.         case -1: {iffp = DOS_ERROR; break; }
  203.         case 0:  {iffp = BAD_IFF;   break; }
  204.         default: {
  205.         context->position   += nBytes;
  206.         context->bytesSoFar += nBytes;
  207.         }
  208.         }
  209.  
  210.     return(iffp);
  211.     }
  212.  
  213. /* ---------- SkipGroup ------------------------------------------------*/
  214. IFFP SkipGroup(context)  GroupContext *context;  {
  215.     }    /* Nothing to do, thanks to GetChunkHdr */
  216.  
  217. /* ---------- ReadIFF --------------------------------------------------*/
  218. IFFP ReadIFF(file, clientFrame)  BPTR file;  ClientFrame *clientFrame;  {
  219.     /*CompilerBug register*/ IFFP iffp;
  220.     GroupContext context;
  221.  
  222.     iffp = OpenRIFF(file, &context);
  223.     context.clientFrame = clientFrame;
  224.  
  225.     if (iffp == IFF_OKAY)
  226.     switch (iffp = GetChunkHdr(&context)) {
  227.         case FORM: { iffp = (*clientFrame->getForm)(&context); break; }
  228.         case LIST: { iffp = (*clientFrame->getList)(&context); break; }
  229.         case CAT : { iffp = (*clientFrame->getCat )(&context); break; }
  230.         /* default: Includes IFF_DONE, BAD_IFF, NOT_IFF... */
  231.         }
  232.  
  233.     CloseRGroup(&context);
  234.  
  235.     if (iffp > 0)        /* Make sure we don't return an ID.*/
  236.     iffp = NOT_IFF;        /* GetChunkHdr should've caught this.*/
  237.     return(iffp);
  238.     }
  239.  
  240. /* ---------- ReadIList ------------------------------------------------*/
  241. IFFP ReadIList(parent, clientFrame)
  242.     GroupContext *parent;  ClientFrame *clientFrame; {
  243.     GroupContext listContext;
  244.     IFFP iffp;
  245.     BOOL propOk = TRUE;
  246.  
  247.     iffp = OpenRGroup(parent, &listContext);
  248.     CheckIFFP();
  249.  
  250.     /* One special case test lets us handle CATs as well as LISTs.*/
  251.     if (parent->ckHdr.ckID == CAT)
  252.     propOk = FALSE;
  253.     else
  254.     listContext.clientFrame = clientFrame;
  255.  
  256.     do {
  257.     switch (iffp = GetChunkHdr(&listContext)) {
  258.         case PROP: {
  259.         if (propOk)
  260.             iffp = (*clientFrame->getProp)(&listContext);
  261.         else
  262.             iffp = BAD_IFF;
  263.         break;
  264.         }
  265.         case FORM: { iffp = (*clientFrame->getForm)(&listContext); break; }
  266.         case LIST: { iffp = (*clientFrame->getList)(&listContext); break; }
  267.         case CAT : { iffp = (*clientFrame->getCat )(&listContext); break; }
  268.         /* default: Includes END_MARK, IFF_DONE, BAD_IFF, NOT_IFF... */
  269.         }
  270.     if (listContext.ckHdr.ckID != PROP)
  271.         propOk = FALSE;    /* No PROPs allowed after this point.*/
  272.     } while (iffp == IFF_OKAY);
  273.  
  274.     CloseRGroup(&listContext);
  275.  
  276.     if (iffp > 0)    /* Only chunk types above are allowed in a LIST/CAT.*/
  277.     iffp = BAD_IFF;
  278.     return(iffp == END_MARK ? IFF_OKAY : iffp);
  279.     }
  280.  
  281. /* ---------- ReadICat -------------------------------------------------*/
  282. /* By special arrangement with the ReadIList implement'n, this is trivial.*/
  283. IFFP ReadICat(parent)  GroupContext *parent;  {
  284.     return( ReadIList(parent, NULL) );
  285.     }
  286.  
  287. /* ---------- GetFChunkHdr ---------------------------------------------*/
  288. ID GetFChunkHdr(context)   GroupContext *context; {
  289.     register ID id;
  290.  
  291.     id = GetChunkHdr(context);
  292.     if (id == PROP)
  293.     context->ckHdr.ckID = id = BAD_IFF;
  294.     return(id);
  295.     }
  296.  
  297. /* ---------- GetF1ChunkHdr ---------------------------------------------*/
  298. ID GetF1ChunkHdr(context)   GroupContext *context; {
  299.     register ID id;
  300.     register ClientFrame *clientFrame = context->clientFrame;
  301.  
  302.     switch (id = GetChunkHdr(context))  {
  303.     case PROP: { id = BAD_IFF; break; }
  304.     case FORM: { id = (*clientFrame->getForm)(context); break; }
  305.     case LIST: { id = (*clientFrame->getList)(context); break; }
  306.     case CAT : { id = (*clientFrame->getCat )(context); break; }
  307.     /* Default: let the caller handle other chunks */
  308.     }
  309.     return(context->ckHdr.ckID = id);
  310.     }
  311.  
  312. /* ---------- GetPChunkHdr ---------------------------------------------*/
  313. ID GetPChunkHdr(context)   GroupContext *context; {
  314.     register ID id;
  315.  
  316.     id = GetChunkHdr(context);
  317.     switch (id) {
  318.     case LIST:  case FORM:  case PROP:  case CAT:  {
  319.         id = context->ckHdr.ckID = BAD_IFF;
  320.         break; }
  321.     }
  322.     return(id);
  323.     }
  324.  
  325.